package com.controller;


import com.alibaba.fastjson.JSON;
import com.pojo.User;
import com.service.MenuService;
import com.util.GetTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Map;

@Controller
public class IndexController{

    @Resource
    private MenuService menuService;

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("welcome");
        mv.addObject("msg", "ModelAndView传参。。。");
        return mv;
    }

    @RequestMapping("/index1")
    public String index1(Map map){
        map.put("msg", "index1");
        return "welcome";
    }

    @RequestMapping("/index2")
    public String index2(HttpSession session){
        session.setAttribute("msg", "index2");
        return "welcome";
    }

    @RequestMapping("/index3")
    public String index2(Model model){
        model.addAttribute("msg", "index3");
        return "welcome";
    }

    @RequestMapping("/getUserJson")
    @ResponseBody
    public User getUserJson(@RequestBody User user) throws Exception{
        User user1 = new User();
        user1.setId(1);
        user1.setName("张三");
        user1.setAge(18);
        user1.setTime(GetTime.getTime());
        return user;
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public User getUser(){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setAge(18);
        user.setTime(GetTime.getTime());
        if (user.getId()==1){
            throw new RuntimeException("Runtime异常!!!");
        }
        return user;

    }

    /*@ExceptionHandler(RuntimeException.class)
    public String exception(){
        return "error";
    }*/


    @RequestMapping("/uploads")
    public void uploads(@RequestParam("pName") String pName, @RequestParam("pPath") MultipartFile multipartFile){
        File file = new File("D:\\JavaWeb\\SpringMVCDemo\\web\\picture",multipartFile.getOriginalFilename());
        try{
            multipartFile.transferTo(file);
            System.out.println(pName);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
